﻿using AutoMapper.Internal.Mappers;
using MyProduce.enumeration;
using MyProduce.MyProduce.ProduceDto;
using MyProduce.Produce;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace MyProduce.MyProduce
{

    public  class HybridizationAppService : ApplicationService, IHybridizationAppService
    {
        private readonly IRepository<EweBreeding, int> _pregnancytestrsult;
        private readonly IRepository<Breedingpattern, int> _breedingpattern;
        public HybridizationAppService(IRepository<EweBreeding, int> pregnancytestrsult, IRepository<Breedingpattern, int> breedingpattern)
        {
            _pregnancytestrsult = pregnancytestrsult;
            _breedingpattern = breedingpattern;
        }

        public async Task DeleteAsync(int Aid)
        {
            await _pregnancytestrsult.DeleteAsync(Aid);
        }

        /// <summary>
        /// 配种方式显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<BreedingPatternDto>> GetBreedingPatternShow()
        {
            var result = await _breedingpattern.GetListAsync();
            return ObjectMapper.Map<List<Breedingpattern>, List<BreedingPatternDto>>(result);
        }

        public async Task<List<EstrustypeDto>> GetEstrustypeEnumShow()
        {
            return await Task.Run(() =>
            {
                List<EstrustypeDto> list = new List<EstrustypeDto>();

                foreach (var item in Enum.GetValues(typeof(Estrustype)))
                {
                    list.Add(new EstrustypeDto
                    {
                        Name = item.ToString(),
                        TId = Convert.ToInt32(item)
                    });
                }

                return list;
            });
        }
        public async Task<List<HybridizationshowDto>> GethybridizationShow(string? Overbit)
        {
            var result = await _pregnancytestrsult.GetListAsync();
            var sheeclass = await GetEstrustypeEnumShow();
            var list=(from a in result
                      join b in sheeclass on a.Speciesestrustype equals b.TId
                      select new HybridizationshowDto
                      {
                          Id = a.Id,
                          Surrenderdate = a.Surrenderdate,
                          principal = a.principal,
                          field = a.field,
                          Transfertobuilding = a.Transfertobuilding,
                          Breedingpattern = a.Breedingpattern,
                          Dateofbreeding = a.Dateofbreeding,
                          BreedfemaleRAMS = a.BreedfemaleRAMS,
                          Overbit = a.Overbit,
                          Cottage = a.Cottage,
                          Drivefield = a.Drivefield,
                          Roundtype = a.Roundtype,
                          Speciesestrustype = a.Speciesestrustype,
                          Variety = a.Variety,
                          SpeciesestrustypenAME = b.Name
                      }).ToList();
            if (!string.IsNullOrEmpty(Overbit))
            {
                list = list.Where(x => x.Overbit.Contains(Overbit)).ToList();
            }
                      
            return list;
        }
        /// <summary>
        /// 配种详情
        /// </summary>
        /// <param name="eid"></param>
        /// <returns></returns>

        public async Task<HybridizationDto> GetPrentalFindaa(int eid)
        {
            var result = await _pregnancytestrsult.GetAsync(eid);
            return ObjectMapper.Map<EweBreeding, HybridizationDto>(result);
        }


        /// <summary>
        /// 添加配种
        /// </summary>
        /// <param name="appearanceDTO"></param>
        /// <returns></returns>

        public async Task<EarResult<HybridizationDto>> PrentalAdds(HybridizationDto appearanceDTO)
        {
            EweBreeding ewebreding = new EweBreeding();
            ewebreding.Surrenderdate = appearanceDTO.Surrenderdate;//转舍日期
            ewebreding.principal = appearanceDTO.principal;//负责人
            ewebreding.field = appearanceDTO.field;//原栏位
            ewebreding.Transfertobuilding=appearanceDTO.Transfertobuilding;//转入栋舍
            ewebreding.Breedingpattern = appearanceDTO.Breedingpattern;//配种方式
            ewebreding.Dateofbreeding=appearanceDTO.Dateofbreeding;//配种日期
            ewebreding.BreedfemaleRAMS=appearanceDTO.BreedfemaleRAMS;//配种公羊
            ewebreding.Overbit=appearanceDTO.Overbit;//种母耳号
            ewebreding.Cottage=appearanceDTO.Cottage;//原栋舍
            ewebreding.Drivefield=appearanceDTO.Drivefield;//转入栏位
            ewebreding.Roundtype=appearanceDTO.Roundtype;//转舍类型
            ewebreding.Speciesestrustype = appearanceDTO.Speciesestrustype;//发情类型
            ewebreding.Variety=appearanceDTO.Variety;//品种
            var result = await _pregnancytestrsult.InsertAsync(ewebreding);
            var res = ObjectMapper.Map<EweBreeding, HybridizationDto>(result);
            if (res != null)
            {
                return new EarResult<HybridizationDto>() { Code = 200, Message = "添加成功" };
            }
            else
            {
                return new EarResult<HybridizationDto>() { Code = 500, Message = "添加失败" };
            }
        }

        public async Task<EarResult<HybridizationDto>> UpdateAsync(HybridizationDto dto)
        {
            var res = await _pregnancytestrsult.FindAsync(dto.Id);
            if (res == null)
            {
                throw new UserFriendlyException("没有找到对应的用户");
            }
            ObjectMapper.Map(dto, res);
            var count = await _pregnancytestrsult.UpdateAsync(res);
            var result = ObjectMapper.Map<EweBreeding, HybridizationDto>(count);

            if (result != null)
            {
                return new EarResult<HybridizationDto>() { Code = 200, Message = "修改成功！" };
            }
            else
            {
                return new EarResult<HybridizationDto>() { Code = 500, Message = "修改失败！" };
            }
        }
    }
}
